package com.bjdd.jdy.ui.register;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.User;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.StringUtils;
import com.bjdd.jdy.util.T;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity<RegisterPresenter, RegisterModel> implements RegisterContract.View {


    @Bind(R.id.et_register_name)
    EditText etRegisterName;
    @Bind(R.id.et_register_code)
    EditText etRegisterCode;
    @Bind(R.id.btn_register_getcode)
    Button btnRegisterGetcode;
    @Bind(R.id.et_register_pwd)
    EditText etRegisterPwd;
    @Bind(R.id.et_register_confirm_pwd)
    EditText etRegisterConfirmPwd;
    @Bind(R.id.btn_register)
    AppCompatButton btnRegister;
    @Bind(R.id.tv_reg_title)
    TextView tvTitle;
    @Bind(R.id.tv_top_title)
    TextView topTitle;
    private CountDownTimer timer;
    private int type;

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.primary),0);
        btnRegisterGetcode.setClickable(true);
        Bundle bundle = getIntent().getExtras();
        String title = bundle.getString("title");
        type = bundle.getInt("type");
        tvTitle.setText(title);
        topTitle.setText(title);
        if (type == 1){
            btnRegister.setText("确认修改");
        }
    }

    @Override
    public void registerSuccess(User user) {
        if (timer!=null){
            timer.cancel();
            timer = null;
        }
        JumpUtil.toMain(context);
        finish();
    }

    @Override
    public void resetSuccess() {
        if (timer!=null){
            timer.cancel();
            timer = null;
        }
        finish();
    }


    @OnClick({R.id.btn_register_getcode, R.id.btn_register,R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_register_getcode:
                if (TextUtils.isEmpty(etRegisterName.getText().toString())){
                    T.show("请输入手机号码");
                }else if (StringUtils.isPhone(etRegisterName.getText().toString())){
                    if (type == 0){
                        presenter.getCode(etRegisterName.getText().toString());
                    }else if (type == 1){
                        presenter.getForgetCode(etRegisterName.getText().toString());
                    }
                    long timeAll = 60*1000;
                    long timeSpace = 1000;
                    btnRegisterGetcode.setClickable(false);
                    timer = new CountDownTimer(timeAll,timeSpace) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            btnRegisterGetcode.setText(millisUntilFinished/timeSpace+"s后可重发");
                        }

                        @Override
                        public void onFinish() {
                            btnRegisterGetcode.setClickable(true);
                            btnRegisterGetcode.setText("发送验证码");
                        }
                    };
                    timer.start();
                }else {
                    T.show("请检查手机号码格式");
                }
                break;
            case R.id.btn_register:
                String pwd1 = etRegisterPwd.getText().toString();
                String pwd2 = etRegisterConfirmPwd.getText().toString();
                if (TextUtils.isEmpty(etRegisterCode.getText().toString())){
                    T.show("请输入验证码");
                }else if (TextUtils.isEmpty(pwd1) || TextUtils.isEmpty(pwd2) || !TextUtils.equals(pwd1,pwd2)){
                    T.show("请检查密码");
                }else if (pwd1.length() < 6)
                {
                    T.show("密码不能少于6位");
                } else {
                    if (type == 0){
                        presenter.register(etRegisterCode.getText().toString(),etRegisterName.getText().toString(),pwd1);
                    }else {
                        presenter.forgetCommit(etRegisterCode.getText().toString(),etRegisterName.getText().toString(),pwd1);
                    }
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }
}
